// xc_lshop/shop/user/money/exchange.js
const app = getApp()
var actmember = require('../../../../xc/model/member')

Page({
  mixins: [require('../../../../xc/mixins/basicMixin')],
  /**
   * 页面的初始数据
   */
  data: {
    servicemoney:0,
    avtval:"0"
  },
  //全部转换
  allchange(){
    var member=this.data.member

    if(member.memberinfo && member.memberinfo.money){
      var mun=member.memberinfo.money/100
      mun= mun.toFixed(2)
      this.calculation(mun)

    }else{
      this.login() 
    }
  },
  //金额
  changemoney(e) {
    

    this.calculation(e.detail.value)
  },
  calculation(value){
    var member=this.data.member
    var maxmoney=0
    var minmoney=0
    var rate=0;
    if(this.data.rebateconfig){
      if(this.data.rebateconfig['status']==-1 ){
        this.error("返利团未配制", true)
        return;
      }
      if( this.data.rebateconfig['startmoney']){
        minmoney=  this.data.rebateconfig['startmoney']
      }else{
        minmoney= 0;
      }

      if(this.data.rebateconfig['rate']){
        rate= this.data.rebateconfig['rate']
      }else{
        rate=0
      }

    }
  
    if(member.memberinfo && member.memberinfo.money){
      var mun=member.memberinfo.money/100
      maxmoney= mun.toFixed(2)
     

      if(maxmoney<minmoney){
        this.error("最小起换金额为"+minmoney, false)
        return;
      }
    }
    
    if (value) {
      var num = value * 1;
      var inputval = num.toFixed(2)

      if(inputval<minmoney){
        inputval=minmoney
      }

      if(inputval>maxmoney){
        inputval=maxmoney
      }
     
      var servicemoney=inputval* rate * 0.001
    
      servicemoney=servicemoney.toFixed(2)
    
      var avtval=inputval - servicemoney
      avtval=avtval.toFixed(2)
    
      this.setData({
        servicemoney:servicemoney,
        avtval:avtval,
        value: inputval,
      })
    }
  },
  error(message, back) {
    var homeurl = app.globalData.configdata.basic.homeurl
    wx.showModal({
      title: "错误",
      content: message,
      showCancel: false,
      success(res) {
        if (res.confirm) {
          if (back) {
    console.log('wx.reLaunch0000000000000000000')
         
            wx.reLaunch({
              url: homeurl,
            })
          }
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  xcloaddata: function (options, config) {
   
    var update = {};
    if (config.rebate) {
      update['rebateconfig'] = config.rebate;
    }
    this.setData(update)
  },
  afterlogin: function (options, config, userinfo) {
    var that = this;
    that.setData({
      "member": userinfo
    })
    if (userinfo.member_id == 0) { //未登录
      this.login()
    }
  },
  // 登录注册
  login() {
    var that = this;
    app.xc_sys_config.authop.oppower("login").then(function (res) {
      that.setData({
        "member": app.globalData.member
      })
    })
  },
  change(){
    if(this.data.value){
      var value=this.data.value;
    }else{
      this.error("请输入转换金额",false)
      return;
    }
    var that=this;
    actmember.exchange(value).then(res => {
      if(res['status']==1){
        app.globalData.member=res['obj'];
        that.setData({
          member:res['obj']
        })
        wx.showToast({
          title: '转换成功',
          icon: 'success',
          duration: 2000
        })
      }else{
        var messge="操作失败";
        if(res['messge']){
          messge=res['messge']
        }
        wx.showToast({
          icon:"error",
          title: messge,
        })
      }

    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  
})